#!/usr/bin/env python
t1 = 'if a < b then a=a+b endif$'
charindex = -1
keywordDic={'if':1,'then':2,'endif':3}
typeDic={'key':1,'id':2,'op':3,'int':4,'real':5} #key,id,op,int,real=range(5)

class token:
    def __init__(self,type,wordNo):
        self.type = typeDic[type]
        self.word = wordNo
    def __str__(self):
        global symbolTable
        t=''
        t2=''
        for i in symbolTable:
            if self.word == i[2]:
                t = i[0]
                break
        for i in typeDic.iterkeys():
            if typeDic[i] == self.type:
                t2 = i
                break
        return 'word = %s, type = %s'%(t,i)

tokenList = []
symbolTable=[['if',2,1],['then',2,2],['endif',2,3]]
idindex=100
def prtTkn():
    for i in symbolTable:
        yield f(i)
        
def insertSymbolTab(word,type):
    for i in symbolTable:
        if word in i:
            return i[2]
    temp = genNum()
    symbolTable.append([word,typeDic[type],temp])
    return temp

def genNum():
    global idindex
    idindex += 1
    return idindex
def getchar():
    global charindex    
    charindex += 1
    print t1[charindex]
    return t1[charindex]
def ungetchar():
    global charindex
    charindex -= 1
def insertTkn(type,word):
    tokenList.append(token(type,word))
def scan(t1):
    while charindex < len(t1):
        tkn=getchar()
        if tkn == '+':
            tkn=getchar()
            if tkn == '=':
                insertTkn('op','+=')
            insertTkn('op','+')    
            ungetchar()
        elif tkn == '-':
            tkn=getchar()
            if tkn == '=':
                insertTkn('op','-=')
            insertTkn('op','-')    
            ungetchar()
        elif tkn == '*':
            insertTkn('op','*')
        elif tkn == '/':
            insertTkn('op','/')
        elif tkn == '=':
            tkn=getchar()
            if tkn == '=':
                insertTkn('op','==')
            insertTkn('op','=')    
            ungetchar()
        elif tkn == '<':
            insertTkn('op','<')
        elif tkn == '>':
            insertTkn('op','>')
        elif tkn == '&':
            tkn=getchar()            
            if tkn == '&':
                insertTkn('op','&&')
            else:
                print 'error : & could be &&'
                break
        elif tkn == '|':
            tkn=getchar()
            if tkn == '|':
                insertTkn('op','||')
            else:
                print 'error : | could be ||'
                break
        elif tkn <= '9' and tkn >= '0':
            temp=tkn
            tkn=getchar()
            while tkn <= '9' and tkn >= '0':
                temp+=tkn
                tkn=getchar()
            if tkn == '.':
                if  tkn <= '9' and tkn >= '0':
                    temp+=tkn
                    tkn=getchar()
                    while tkn <= '9' and tkn >= '0':
                        temp+=tkn
                        tkn=getchar()
                    temp = insertSymbolTab(temp,'real')
                    insertTkn('real',temp)
                    ungetchar()
                else:
                    print 'error : follow of "." could be digit'
                    break                    
            else:
                temp = insertSymbolTab(temp,'int')
                insertTkn('int',temp)
                ungetchar()
        elif (tkn <= 'z' and tkn >= 'a') or (tkn <= 'Z' and tkn >= 'A'):
            temp=tkn
            tkn=getchar()
            while (tkn <= 'z' and tkn >= 'a') or (tkn <= 'Z' and tkn >= 'A') or (tkn <= '9' and tkn >= '0'):
                temp+=tkn
                tkn=getchar()
            if temp in keywordDic:
                insertTkn('key',keywordDic[temp])
                ungetchar()
            else:
                temp = insertSymbolTab(temp,'id')
                insertTkn('id',temp)
                ungetchar()
        elif tkn == ' ': 
            pass
        elif tkn == '$':
            for i in tokenList:
                print i
            break
        else:
            print 'error : not support charator ',tkn
            break
    return t1
scan(t1)

